package com.harmontronics.hanma.paas.paas.utils;

import cn.hutool.core.util.ObjectUtil;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.Map;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Assertions;
import org.springframework.util.StringUtils;

/**
 * @Title:
 * @ClassName: ConvertUtil
 * @Description:
 * @Author: tylr
 * @Date: 2021/6/2 19:25
 * @Version: 1.0.0
 */
@Slf4j
public class ConvertUtil {

    public final static  <T> T getValueFromMap(Map<String, Object> params, String paramKey,Class<T> targetClass){
        paramKey = StringUtil.strip(paramKey);
        if(StringUtil.isNotBlank(paramKey) && params.containsKey(paramKey)){
            return convert(params.get(paramKey),targetClass);
        }
        return null;
    }

    public final static String convertToString(Object value) {
        String result = null;
        if (value == null) {
            return result;
        }
        if (value instanceof Integer || value instanceof Long || value instanceof Float || value instanceof Double || value instanceof BigDecimal) {
            result = value.toString();
        }
        if (value instanceof LocalDateTime) {
            result = ((LocalDateTime) value).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        }
        if (value instanceof LocalDate) {
            result = ((LocalDate) value).format(DateTimeFormatter.ofPattern("yyyy-MM-dd")) ;
        }
        if (value instanceof LocalTime) {
            result = ((LocalTime) value).format(DateTimeFormatter.ofPattern("HH:mm:ss"));
        }
        result = value.toString();
        return StringUtil.strip(result);
    }

    public final static<T> T convert(Object value, Class<T> tClass){
        try {
            return convert(convertToString(value),tClass);
        } catch (Exception e) {
            log.error("转换异常", e);
            return null;
        }
    }

    public final static<T> T convert(String valueStr, Class<T> tClass){
        if(tClass == String.class){
            return (T)valueStr;
        }

        if(StringUtils.isEmpty(valueStr)){
            return null;
        }

        try{
            Object object = null;
            if(tClass == Float.class || tClass == float.class){
                object = Float.parseFloat(valueStr);
            }else if(tClass == Double.class || tClass == double.class){
                object = Double.parseDouble(valueStr);
            }else if(tClass == Byte.class || tClass == byte.class){
                object = Byte.parseByte(valueStr);
            }else if(tClass == Short.class || tClass == short.class){
                object = Short.parseShort(valueStr);
            }else if(tClass == Integer.class || tClass == int.class){
                object = Integer.parseInt(valueStr);
            }else if(tClass == Long.class || tClass == long.class){
                object = Long.parseLong(valueStr);
            }else if(tClass == Boolean.class || tClass == boolean.class){
                object = valueStr.equalsIgnoreCase("true") || valueStr.equalsIgnoreCase("1");
            }

            if(object == null){
                return null;
            }

            T result = (T)object;
            return result;
        }catch (Exception e){
            log.error("转换异常", e);
        }

        return null;
    }


    public static void main(String[] args) {
        Assertions.assertTrue(convert(1,Integer.class) instanceof Integer);
        Assertions.assertTrue(convert("1",Integer.class)instanceof Integer);
        Assertions.assertTrue(convert(1.2,Float.class) instanceof  Float);
        Assertions.assertTrue(convert(1.2,Double.class) instanceof  Double);
        Assertions.assertTrue(convert("1.2",Float.class) instanceof Float);
        Assertions.assertTrue(convert(false,Boolean.class) instanceof Boolean);
        Assertions.assertTrue(convert("true",Boolean.class) instanceof Boolean);
        Assertions.assertTrue(convert("test",String.class) instanceof String);
    }
}
